package org.feng.controller;

import cn.dev33.satoken.secure.SaBase64Util;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.feng.constant.RedisConstant;
import org.feng.entity.pojo.User;
import org.feng.entity.vo.UserInfoVO;
import org.feng.pojo.Result;
import org.feng.service.DeptService;
import org.feng.service.JobService;
import org.feng.service.RoleService;
import org.feng.service.UserService;
import org.feng.utils.ExcelUtils;
import org.feng.utils.UserUtil;
import org.feng.utils.condition.Condition;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author feng
 * @since 2021-08-27
 */
@RestController
@RequestMapping("/system/user")
@AllArgsConstructor
@Api("用户接口")
public class UserController {

    private final UserService userService;
    private final JobService jobService;
    private final RoleService roleService;
    private final DeptService deptService;
    private final StringRedisTemplate redisTemplate;

    /**
     * 导出
     */
    @ApiOperation(value = "导出", notes = "导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        ExcelUtils.writeExcel(response, userService.list(), User.class);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/userInfo")
    @ApiOperation("获取用户信息")
    public Result<UserInfoVO> getUserInfo() {
        User user = userService.getById(UserUtil.getUserId());
        UserInfoVO userInfoVO = BeanUtil.toBean(user, UserInfoVO.class);
        if (ObjectUtil.isNotNull(userInfoVO)) {
            userInfoVO.setJob(jobService.getById(user.getJobId()).getName());
            userInfoVO.setRole(roleService.getById(user.getRoleId()).getName());
            userInfoVO.setDept(deptService.getById(user.getDeptId()).getName());
            userInfoVO.setSex(user.getSex() == 1 ? "男" : (user.getSex() == 2 ? "女" : "未知"));
        }
        return Result.success(userInfoVO);
    }


    /**
     * 修改用户信息
     */
    @ApiOperation(value = "修改用户信息", notes = "用户信息")
    @PutMapping("/updateUserInfo")
    public Result<Boolean> updateUserInfo(@RequestBody User user) {
        user.setId(UserUtil.getUserId());
        redisTemplate.opsForValue().set(RedisConstant.USER_INFO + user.getId(), JSON.toJSONString(user));
        return Result.success(userService.updateById(user));
    }

    /**
     * 修改密码
     */
    @ApiOperation(value = "修改密码", notes = "密码")
    @PutMapping("/restPassword")
    public Result<Boolean> restPassword(@RequestParam String oldPassword, @RequestParam String newPassword) {
        User user = userService.getById(UserUtil.getUserId());
        if (!SaBase64Util.encode(oldPassword).equals(user.getPassword())) {
            return Result.error("密码输入错误");
        } else {
            user.setPassword(SaBase64Util.encode(newPassword));
            redisTemplate.opsForValue().set(RedisConstant.USER_INFO + user.getId(), JSON.toJSONString(user));
            return Result.success(userService.updateById(user));
        }
    }

    /**
     * 列表
     */
    @ApiOperation(value = "列表", notes = "全部数据")
    @GetMapping
    public Result<List<User>> list(User user) {
        return Result.success(userService.list(Condition.getQueryWrapper(user)));
    }

    /**
     * 分页列表
     */
    @ApiOperation(value = "分页列表", notes = "分页数据")
    @GetMapping("/page")
    public Result<IPage<User>> page(User user) {
        return Result.success(userService.page(Condition.getPage(), Condition.getQueryWrapper(user)));
    }

    /**
     * 详情
     */
    @ApiOperation(value = "数据详情", notes = "传输id")
    @GetMapping("/query")
    public Result<User> detail(@RequestParam Long id) {
        return Result.success(userService.getById(id));
    }

    /**
     * 新增
     */
    @ApiOperation(value = "保存数据", notes = "传输对象")
    @PostMapping
    public Result<Boolean> save(@RequestBody User user) {
        return Result.status(userService.save(user));
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新数据", notes = "传输对象")
    @PutMapping
    public Result<Boolean> update(@RequestBody User user) {
        redisTemplate.opsForValue().set(RedisConstant.USER_INFO + user.getId(), JSON.toJSONString(user));
        return Result.status(userService.updateById(user));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除数据", notes = "传输id数组")
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam String ids) {
        return Result.status(userService.removeByIds(Arrays.asList(ids.split(","))));
    }
}

